package com.zyl.Ecs.auth.web.controller;

import com.zyl.Ecs.common.domain.auth.ext.RoleExt;
import com.zyl.Ecs.common.domain.auth.vo.RoleVO;
import com.zyl.Ecs.auth.service.RoleService;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.response.ResultCode;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.web.controller
 * @ClassName: RoleController
 * @Description TODO
 * @Author 翟玉龙
 * @Date 2024/12/4-19:43
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    /**
     * 新增角色
     *
     * @param roleVO 角色信息
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping
    public Result<?> add(@RequestBody RoleVO roleVO) {
        roleService.add(RoleExt.toExt(roleVO));
        return Result.success();
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return {@link Result }<{@link ? }>
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Integer id) {
        roleService.delete(id);
        return Result.success();
    }

    /**
     * 修改角色
     *
     * @param roleVO 角色信息
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping
    public Result<?> update(@RequestBody RoleVO roleVO) {
        roleService.update(RoleExt.toExt(roleVO));
        return Result.success();
    }

    /**
     * 查询角色列表
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    public Result<?> list() {
        return Result.success(Optional.ofNullable(roleService.getList())
                .map(list -> list.stream()
                        .map(RoleVO::toVO)
                        .collect(Collectors.toList()))
                .orElse(null))
                .apply(ResultCode.SUCCESS.getMessage());
    }
}
